package hu.dobrosi.util;

import hu.dobrosi.util.log.Logger;
import hu.dobrosi.util.log.SysoutLogger;

import java.util.Vector;

public class StringUtil {
	public static Logger logger = new SysoutLogger();
	public static Object[] splitString(String str, String splitter) {
		Vector list = new Vector();
		try {
			int beginIndex = 0;
			int endIndex;
			while ((endIndex = str.indexOf(splitter, beginIndex)) != -1) {
				list.addElement(str.substring(beginIndex, endIndex));
				beginIndex = endIndex + 1;
			}
			list.addElement(str.substring(beginIndex));
			
		} catch (Exception e) {
			StringUtil.logger.clear();
			logger.println(e.toString());
		}
		return VectorUtil.toArray(list);
	}

	public static String replaceAll(String str, String oldstr, String newstr) {
		String res = "";
		int beginIndex = 0;
		int endIndex;
		int len = oldstr.length();
		while ((endIndex = str.indexOf(oldstr, beginIndex)) != -1) {
			res += str.substring(beginIndex, endIndex) + newstr;
			beginIndex = endIndex + len;
		}
		res += str.substring(beginIndex);
		return res;
	}

	public static boolean isEmpty(String string) {
		return string == null || "".equals(string);
	}
}
